﻿using System;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Linkman
{
    /// <summary>
    /// CustomLinkman 的摘要说明。
    /// </summary>
    public partial class CustomLinkmanPage : Page
    {
        private int id;

        protected override void OnInit(EventArgs e)
        {
            tbx_Email.TextChanged += tbx_Email_TextChanged;
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                id = (string.IsNullOrEmpty(Request.QueryString["ID"])) ? 0 : Int32.Parse(Request.QueryString["ID"]);
                ViewState["ID"] = id.ToString();
                BindData();
            }
            else
            {
                id = Int32.Parse(ViewState["ID"].ToString());
            }
            if (id == 0)
                btn_OK.Text = "添加";
            else
                btn_OK.Text = "修改";

            base.OnLoad(e);
        }

        private void BindData()
        {
            var mlinkman = new MyLinkman();
            SqlDataReader dr_mlinkman = mlinkman.GetCustomLinkman(id);
            BindTypeList();
            while (dr_mlinkman.Read())
            {
                tbx_Name.Text = dr_mlinkman["Name"].ToString();
                tbx_Age.Text = dr_mlinkman["Age"].ToString();
                tbx_UnitAddress.Text = dr_mlinkman["UnitAddress"].ToString();
                tbx_UnitTelephone.Text = dr_mlinkman["UnitTelephone"].ToString();
                tbx_UnitZip.Text = dr_mlinkman["UnitZip"].ToString();
                tbx_FamilyAddress.Text = dr_mlinkman["FamilyAddress"].ToString();
                tbx_FamilyTelephone.Text = dr_mlinkman["FamilyTelephone"].ToString();
                tbx_FamilyZip.Text = dr_mlinkman["FamilyZip"].ToString();
                tbx_Mobile.Text = dr_mlinkman["Mobile"].ToString();
                tbx_Email.Text = dr_mlinkman["Email"].ToString();
                tbx_Memo.Text = dr_mlinkman["Memo"].ToString();

                if (Convert.ToBoolean(dr_mlinkman["Gender"]))
                {
                    ddl_Gender.SelectedIndex = 0;
                }
                else
                {
                    ddl_Gender.SelectedIndex = 1;
                }

                SqlDataReader dr_type = mlinkman.GetCustomLinkmanType(id);
                while (dr_type.Read())
                {
                    foreach (DataListItem dli in dlt_Type.Items)
                    {
                        if (((CheckBox) dli.FindControl("Checkbox1")).Text == dr_type["Type"].ToString())
                            ((CheckBox) dli.FindControl("Checkbox1")).Checked = true;
                    }
                }
                dr_type.Close();
            }
            dr_mlinkman.Close();
        }

        //bound to linkmantype
        private void BindTypeList()
        {
            using (var dr = MyLinkman.GetCustomLinkmanType())
            {
                dlt_Type.DataSource = dr;
                dlt_Type.DataKeyField = "ID";
                dlt_Type.DataBind();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            var mlinkman = new MyLinkman();
            var clinkman = new CustomLinkman();

            clinkman.Name = tbx_Name.Text;
            clinkman.Age = tbx_Age.Text;
            clinkman.Gender = (ddl_Gender.Items[ddl_Gender.SelectedIndex].Value == "1") ? true : false;
            clinkman.UnitAddress = tbx_UnitAddress.Text;
            clinkman.UnitTelephone = tbx_UnitTelephone.Text;
            clinkman.UnitZip = tbx_UnitZip.Text;
            clinkman.FamilyAddress = tbx_FamilyAddress.Text;
            clinkman.FamilyTelephone = tbx_FamilyTelephone.Text;
            clinkman.FamilyZip = tbx_FamilyZip.Text;
            clinkman.Email = tbx_Email.Text;
            clinkman.Mobile = tbx_Mobile.Text;
            clinkman.Memo = tbx_Memo.Text;
            clinkman.ID = id;

            for (int i = 0; i < dlt_Type.Items.Count; i++)
            {
                if (((CheckBox) dlt_Type.Items[i].Controls[1]).Checked)
                {
                    clinkman.Type += dlt_Type.DataKeys[i] + ",";
                }
            }

            mlinkman.UpdateCustomLinkman(clinkman);
            mlinkman.UpdateCustomLinkmanType(clinkman);

            BindData();

            if (id == 0)
                Response.Write("<script>alert('添加成功!');</script>");
            else
                Response.Write("<script>alert('修改成功!');</script>");
        }

        private void tbx_Email_TextChanged(object sender, EventArgs e)
        {
        }
    }
}